/*
 * Copyright (C) 2019 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.slice.tabbar;

import com.xuexiang.xui_lib.tabbar.MultiTabControlView;
import com.xuexiang.xui_lib.tabbar.TabControlView;
import com.xuexiang.xui_lib.util.ResUtils;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.page.base.Page;
import com.xuexiang.xuidemo.slice.BaseAbilitySlice;
import com.xuexiang.xuidemo.util.XToastUtils;

/**
 * @author xuexiang
 * @since 2019/1/3 上午12:41
 */
@Page(name = "TabControlView\n选项卡控制滑块")
public class TabControlViewSlice extends BaseAbilitySlice {
    TabControlView mControlView1;
    TabControlView mControlView2;
    TabControlView mControlView3;
    TabControlView mTabControlView;
    MultiTabControlView mMultiTabControlView;

    @Override
    protected int getUIContentLayoutId() {
        return ResourceTable.Layout_slice_tabcontrolview;
    }

    @Override
    protected int getTitleBarId() {
        return ResourceTable.Id_title_bar;
    }

    @Override
    protected void initComponents() {
        mControlView1 = (TabControlView) findComponentById(ResourceTable.Id_controlView1);
        mControlView2 = (TabControlView) findComponentById(ResourceTable.Id_controlView2);
        mControlView3 = (TabControlView) findComponentById(ResourceTable.Id_controlView3);
        mTabControlView = (TabControlView) findComponentById(ResourceTable.Id_tcv_select);
        mMultiTabControlView = (MultiTabControlView) findComponentById(ResourceTable.Id_tcv_multi_select);
        initTabControlView();
        initMultiTabControlView();
    }

    private void initTabControlView() {
        try {
            String[] option = null;
            if (ResUtils.getStringArray(ResourceTable.Strarray_three_state_option).isPresent()) {
                option = ResUtils.getStringArray(ResourceTable.Strarray_three_state_option).get();
            }
            String[] course = null;
            if (ResUtils.getStringArray(ResourceTable.Strarray_course_param_option).isPresent()) {
                course = ResUtils.getStringArray(ResourceTable.Strarray_course_param_option).get();
            }
            String[] value = null;
            if (ResUtils.getStringArray(ResourceTable.Strarray_course_param_value).isPresent()) {
                value = ResUtils.getStringArray(ResourceTable.Strarray_course_param_value).get();
            }
            mControlView1.setItems(option, null);
            mControlView2.setItems(option, null);
            mControlView3.setItems(option, null);
            mTabControlView.setItems(course, value);
            mTabControlView.setDefaultSelection(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mTabControlView.setOnTabSelectionChangedListener((title, value) -> {
            XToastUtils.toast("选中了：" + title + ", 选中的值为：" + value);
        });
    }

    private void initMultiTabControlView() {
        try {
            String[] course = null;
            if (ResUtils.getStringArray(ResourceTable.Strarray_course_param_option).isPresent()) {
                course = ResUtils.getStringArray(ResourceTable.Strarray_course_param_option).get();
            }
            String[] value = null;
            if (ResUtils.getStringArray(ResourceTable.Strarray_course_param_value).isPresent()) {
                value = ResUtils.getStringArray(ResourceTable.Strarray_course_param_value).get();
            }
            mMultiTabControlView.setItems(course, value);
            mMultiTabControlView.setDefaultSelection(1, 2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mMultiTabControlView.setOnMultiTabSelectionChangedListener((title, value, isChecked) -> {
            XToastUtils.toast((isChecked ? "选中了：" : "取消了：") + title + ", 值为：" + value);
        });
    }
}
